package com.devcon5.StudentPlanner;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import baseclasses.Course;
import database.CourseDAO;

public class ViewCourses extends Activity {
	
	//Widget variables
	private ListView listview;
	private ArrayList<Course> courses;
	private Context context = this;
	private TextView title;
	private CourseAdapter adapter;
	

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewcourses);
        
        //Assign variables to UI elements
        listview = (ListView)findViewById(R.id.viewcourses_listview);
		title = (TextView)findViewById(R.id.viewcourses_title);

		//get array list of courses from database
		courses = CourseDAO.getCourses(context);
		adapter = new CourseAdapter(context, android.R.layout.simple_list_item_1, courses);
		listview.setAdapter(adapter);
		title.setText("Displaying "+courses.size()+" course(s)");
		
		//set OnLongClickListener to edit or delete courses on long click
		listview.setOnItemClickListener(new OnItemClickListener(){

			
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				final Course course = courses.get(arg2);
				final int index = arg2;

				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setTitle(course.getName());
				builder.setIcon(R.drawable.courses);
				builder.setItems(new CharSequence[]{"Edit","Delete"}, new OnClickListener(){

					
					public void onClick(DialogInterface arg0, int which) {


						final int EDIT = 0;
						final int DELETE = 1;

						switch(which){

						case EDIT:
							Intent editIntent = new Intent(context, EditCourse.class);
							//include course id in the intent to send to the edit course activity
							editIntent.putExtra("courseid", course.getId());
							startActivity(editIntent);
							return;

						case DELETE:
							//display delete warning on menu selection
							AlertDialog.Builder builder2 = new AlertDialog.Builder(context);
							builder2.setTitle("Delete?");
							builder2.setIcon(android.R.drawable.ic_delete);
							builder2.setMessage("Are you sure you want to delete "+course.getName()+"?" +
									"\n\nThis WILL delete all assignments and grades associated with it!");
							builder2.setPositiveButton("Delete", new OnClickListener(){
								
								public void onClick(DialogInterface dialog,
										int which) {
									//delete all assignments with the course id
									if(CourseDAO.deleteCourse(context, course)==0){
										Toast.makeText(context, "Error deleting course from database...", Toast.LENGTH_LONG).show();
									}else{
										
										courses.remove(index);
										adapter.notifyDataSetChanged();
										title.setText("Displaying "+courses.size()+" course(s)");
									}
								}
							});
							builder2.setNegativeButton("Cancel", null);
							AlertDialog alert = builder2.create();
							alert.show();
							return;
						}
					}
				});
				AlertDialog alert = builder.create();
				alert.show();

			}

		});

	}
	

	//updates list view to show current list in database
	public void updateListView(){

		courses = CourseDAO.getCourses(context);
		adapter = new CourseAdapter(context, android.R.layout.simple_list_item_1, courses);
		listview.setAdapter(adapter);
		title.setText("Displaying "+courses.size()+" course(s)");
	}
	
	//on activity resume update list view
	@Override 
	public void onResume(){
		super.onResume();
		updateListView();
	}
	
	//Course ListView with ArrayAdapter
	private class CourseAdapter extends ArrayAdapter<Course> {

		private ArrayList<Course> courses;

		public CourseAdapter(Context context, int textViewResourceId, ArrayList<Course> courses) {
			super(context, textViewResourceId, courses);
			this.courses = courses;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.courseviewsimple, null);	//instantiate the courses in a simple view
			}
			Course course = courses.get(position);	//Set the associated textviews = to the course's data
			if (course != null) {

				TextView courseName = (TextView) v.findViewById(R.id.courseviewsimple_course);
				TextView courseLocation = (TextView) v.findViewById(R.id.courseviewsimple_loc);
				TextView weekDays = (TextView) v.findViewById(R.id.courseviewsimple_weekdays);
				TextView time = (TextView) v.findViewById(R.id.courseviewsimple_time);

				if (courseName != null) {
					courseName.setText(course.getName());
				}
				if (courseLocation != null){
					courseLocation.setText(course.getLocation());
				}
				if (weekDays != null) {
					weekDays.setText(course.getWeekDays());                            
				}
				if (time != null) {
					time.setText(course.getStartTimeString("hh:mm"));                            
				}
			
			}
			return v;
		}
	}
	

	//---------------------------------------------------------
	//
	//						MENU METHODS
	//
	//---------------------------------------------------------
		@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.viewcoursesmenu, menu);
		return true;
	}

	//handle item selection
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.addcourse:
			startActivity(new Intent(context, AddCourse.class));
			return true;
		default:
			return super.onOptionsItemSelected(item);
			}
		}
	}	
